<?php

namespace app\admin\controller;

use think\facade\Request;
use app\admin\service\MoneyService;
use app\common\Common;
use app\common\Error;

class Money extends Base
{
    private $service;

    public function __construct(MoneyService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染列表页面模板
    public function index()
    {
        return view();
    }

    // 渲染详情页面模板
    public function info()
    {
        return view();
    }

    // 充值设置列表
    public function list()
    {
        $params = Request::param();

        $data  = $this->service->rechargeSettingsList($params);

        return Common::success($data);
    }

    // 充值详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Money::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data  = $this->service->rechargeSettingsDetail($params['id']);

        return Common::success($data);
    }

    // 添加充值设置
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Money::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editRechargeSettings($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 修改充值设置
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Money::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editRechargeSettings($params);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 删除
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Money::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteRechargeSettings($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 获取平台类型
    public function getPlatformType()
    {
        $data = config('enum.platform_type');
        return Common::success($data);
    }

    // 获取充值类型
    public function getRechargeType()
    {
        $params = Request::param();

        if ($params['type'] == 1) {
            $data = config('enum.recharge_type_1');
        } else {
            $data = config('enum.recharge_type_2');
        }

        return Common::success($data);
    }

    // 渲染充值模板数据页面
    public function templatePage()
    {
        return view();
    }

    // 充值模板数据
    public function templateList()
    {
        $params = Request::param();
        $data = $this->service->rechargeTemplateList($params);
        return Common::success($data);
    }

    // 充值限制开关显示页面
    public function rechargeLimit()
    {
        return view();
    }

    // 充值限制开关
    public function rechargeLimitSwitch()
    {
        $params = Request::param();
        $data = $this->service->rechargeLimitSwitchSetting($params);
        if (empty($data)) {
            return Common::fail(Error::FAIL_MESSAGE);
        }
        return Common::success($data);
    }

    // 充值金额列表新增是否启用
    public function rechargeMoneySwitch()
    {
        $params = Request::param();
        $data = $this->service->rechargeMoneySwitchSetting($params);

        if (!$data) {
            return Common::fail();
        }

        return Common::success();
    }

    public function getRechargeTemplateLimit(Request $request)
    {
        $params = [
            'id' => $request::get('id'),
            'recharge_name' => $request::get('recharge_name'),
            'page' => $request::get('page', 1),
            'limit' => $request::get('limit', 20)
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('get_recharge_template_limit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getRechargeTemplateLimit($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    public function addRechargeTemplateLimit(Request $request)
    {
        $params = [
            'recharge_name' => $request::post('recharge_name'),
            'recharge_day' => $request::post('recharge_day'),
            'recharge_amount' => $request::post('recharge_amount'),
            'recharge_set' => $request::post('recharge_set'),
            'recharge_white_ids' => $request::post('recharge_white_ids')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('add_recharge_template_limit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->addRechargeTemplateLimit($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    public function deleteRechargeTemplateLimit(Request $request)
    {
        $params = [
            'id' => $request::post('id')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('delete_recharge_template_limit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->deleteRechargeTemplateLimit($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    public function updateRechargeTemplateLimit(Request $request)
    {
        $params = [
            'id' => $request::post('id'),
            'recharge_white_ids' => $request::post('recharge_white_ids')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('update_recharge_template_limit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->updateRechargeTemplateLimit($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    public function updateRechargeTemplateLimitVideo(Request $request)
    {
        $params = [
            'id' => $request::post('id'),
            'video_id' => $request::post('video_id')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('update_recharge_template_limit_video')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->updateRechargeTemplateLimitVideo($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success();
    }

    public function getRechargeTemplateLimitDetail(Request $request)
    {
        $params = [
            'id' => $request::get('id')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('get_recharge_template_limit_detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getRechargeTemplateLimitDetail($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    public function getRechargeTemplateLimitVideoDetail(Request $request)
    {
        $params = [
            'video_id' => $request::get('video_id')
        ];

        try {
            validate(\app\admin\validate\Money::class)->scene('get_recharge_template_limit_video_detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getRechargeTemplateLimitVideoDetail($params);

        if (isset($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    // 获取自定义充值模板下拉列表
    public function rechargeTemplateSelect()
    {
        $params = Request::param();
        $data = $this->service->getRechargeTemplateSelectData($params['video_id']);

        return Common::success($data);
    }

    public function limitation_info()
    {
        return view();
    }


    public function limitation()
    {
        return view();
    }

    // 获取充值限制模板下拉列表
    public function limitTemplateSelect()
    {
        $data = $this->service->getRechargeLimitTemplateSelectData();

        return Common::success($data);
    }
}
